package com.demo.springcloud.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = ArrayIndexOutOfBoundsException.class)
    @ResponseBody
    GlobalResult onException(Exception e){
        LOG.error(e.getLocalizedMessage());
        GlobalResult globalResult = GlobalResult.fail(e.getMessage());
        return globalResult;
    }
}
